package com.definity.toolkit.ui.widget;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JToolBar;

import com.definity.toolkit.ui.Align;
import com.definity.toolkit.ui.Body;
import com.definity.toolkit.ui.widget.paint.GradientPaint;
import com.definity.toolkit.ui.widget.paint.Paint;

public class DockPanel extends JToolBar implements Widget<DockPanel> {

	private static final long serialVersionUID = 2809667294175911947L;

	private final Paint paint = GradientPaint.create(Color.decode("#293fa0"), Color.decode("#354174"));
	private final WidgetBuilder b = new WidgetBuilder();

	private String defaultSize = "normal";
	
	public DockPanel() {
		setOpaque(false);
		setBorder(BorderFactory.createEmptyBorder());
		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
	}
	
	public DockPanel button(String id, String iconName, final String text) {
		add(new DockIcon(id, text, iconName));
		return this;
	}
	
	@Override
	public String id() {
		return getName();
	}

	@Override
	public DockPanel id(String id) {
		setName(id);
		return this;
	}

	@Override
	public DockPanel properties(Body<DockPanel> body) {
		body.content(this);
		return this;
	}

	@Override
	public Color background() {
		return getBackground();
	}

	@Override
	public DockPanel background(Color background) {
		setBackground(background);
		return this;
	}

	@Override
	public Color foreground() {
		return getForeground();
	}

	@Override
	public DockPanel foreground(Color foreground) {
		setForeground(foreground);
		return this;
	}

	@Override
	public DockPanel size(int width, int height) {
		setSize(width, height);
		return this;
	}

	@Override
	public Font font() {
		return getFont();
	}

	@Override
	public DockPanel font(Font font) {
		setFont(font);
		return this;
	}

	private class DockIcon extends BorderPanel {

		private static final long serialVersionUID = 2222533241683651377L;
		
		private Icon normalIcon;

		public DockIcon(String id, final String text, String iconName) {
			opaque(false);
			
			normalIcon = WidgetUtils.icon("images/" + iconName + "-" + defaultSize + ".png");
			
			final Label button = b.label(normalIcon).id(id);
			final Label label = b.label(text).foreground(Color.WHITE).horizontalAlignment(Align.CENTER).border(BorderFactory.createEmptyBorder(5, 0, 10, 0));
			label.font(label.font().deriveFont(14f)).size(64, 25);
			
			button.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseEntered(MouseEvent e) {
					label.text(text);
				}

				@Override
				public void mouseExited(MouseEvent e) {
					label.text("");
				}
			});

			center(button);
			bottom(label);
		}
	}

	@Override
	public void paint(Graphics g) {
		paint.paint((Graphics2D) g, this);
		
		super.paint(g);
	}
	
}
